package proyecto.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import proyecto.modelo.Area;

public class AreaDAO {

    public Area insertar(Area ar) {
        String query = "INSERT INTO areaj (nombrearea,adminarea) " + "VALUES (?,?)";
        System.err.println(query);
        Connection con = null;
        PreparedStatement stmt = null;
        try {
            con = ConexionBD.obtenerConexion();
            stmt = con.prepareStatement(query);
            stmt.setString(1, ar.getArea());
            stmt.setString(2, ar.getAdmin());
            int i = stmt.executeUpdate();
            if (i != 1) {
                throw new SQLException("No se pudo insertar");
            }
            stmt.close();
            con.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        return ar;
    }

    public Collection<String> listar() {
        String query = "SELECT nombrearea FROM areaj";
        Collection<String> areas = new ArrayList<String>();
        try {
            Connection con = ConexionBD.obtenerConexion();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                areas.add(rs.getString("nombrearea"));
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return areas;
    }

    public Collection<Integer> listarID(){
        String query = "SELECT idarea FROM areaj";
        Collection<Integer> areas = new ArrayList<Integer>();
        try {
            Connection con = ConexionBD.obtenerConexion();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                areas.add(rs.getInt("idarea"));
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return areas;
    }

    public Collection<String> listarAdmin(){
        String query = "SELECT adminarea FROM areaj";
        Collection<String> areas = new ArrayList<String>();
        try {
            Connection con = ConexionBD.obtenerConexion();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                areas.add(rs.getString("adminarea"));
            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return areas;
    }

    public Area modificar(Area ar) {
        String query = "UPDATE areaj SET nombrearea = ?, adminarea= ? WHERE idarea = ?";
        System.err.println(query);
        Connection con = null;
        PreparedStatement stmt = null;
        try {
            con = ConexionBD.obtenerConexion();
            stmt = con.prepareStatement(query);
            stmt.setString(1, ar.getArea());
            stmt.setString(2, ar.getAdmin());
            stmt.setInt(3, ar.getIdarea());
            int i = stmt.executeUpdate();
            if (i != 1) {
                throw new SQLException("No se pudo modificar");
            }
            stmt.close();
            con.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        return ar;
    }

    public Area eliminar(Area ar) {
        String query = "DELETE FROM areaj WHERE idarea = ?";
        System.err.println(query);
        Connection con = null;
        PreparedStatement stmt = null;
        try {
            con = ConexionBD.obtenerConexion();
            stmt = con.prepareStatement(query);
            stmt.setInt(1, ar.getIdarea());
            int i = stmt.executeUpdate();
            if (i != 1) {
                throw new SQLException("No se pudo eliminar");
            }
            stmt.close();
            con.close();
        } catch (SQLException e) {
            System.err.println(e.getMessage());
        }
        return ar;
    }
}
